package com.daoliuhe.scaffold.service.impl;

import com.daoliuhe.scaffold.mapper.UserGroupUserMapper;
import com.daoliuhe.scaffold.model.UserGroupUser;
import com.daoliuhe.scaffold.service.UserGroupUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;


@Service
public class UserGroupUserServiceImpl implements UserGroupUserService {

	private static final Logger logger = LoggerFactory.getLogger(UserGroupUserServiceImpl.class);
	@Resource
	UserGroupUserMapper userGroupUserMapper;

	@Override
	public Object saveBatchUserForUserGroup(String userIds, String userGroupId) {
		logger.info("saveBatchUserForRole,userIds:{},roleId:{}",userIds,userGroupId);
		Map<String, Object> json = new HashMap<String, Object>();
		boolean success = false;
		String reason = "";
		if (StringUtils.isEmpty(userIds) || StringUtils.isEmpty(userGroupId)) {
			reason = "参数错误!";
		} else {
			List<String> userList = new ArrayList<String>(Arrays.asList(userIds.split(";")));
			for (String userId : userList) {
				UserGroupUser ugu = new UserGroupUser();
				ugu.setUserGroupId(new BigDecimal(userGroupId));
				ugu.setUserId(new BigDecimal(userId));
				if (userGroupUserMapper.select(ugu).isEmpty()) {
					userGroupUserMapper.insert(ugu);
				}
			}
			success = true;
		}
		json.put("success", success);
		json.put("reason", reason);
		return json;
	}

	@Override
	public Object getUserPageData(UserGroupUser ugu) {
		logger.info("getUserPageData,ugu:{}",ugu);
		Map<String, Object> map = new HashMap<String, Object>();
		int total = userGroupUserMapper.getUserPageCount(ugu);
		int curPage = ugu.getPage();
		int rows = ugu.getRows();
		int maxPage = (total + rows - 1) / rows;
		if (curPage > maxPage && maxPage > 0) {
			ugu.setPage(maxPage);
		}
		map.put("total", total);
		map.put("rows", userGroupUserMapper.getUserPageData(ugu));
		return map;
	}

	@Override
	public Object deleteUser(String userIds, String userGroupId) {
		logger.info("deleteForRole,userIds:{},userGroupId:{}",userIds,userGroupId);
		Map<String, Object> json = new HashMap<String, Object>();
		boolean success = false;
		String reason = "";
		if (StringUtils.isEmpty(userIds) || StringUtils.isEmpty(userGroupId)) {
			reason = "参数错误!";
		} else {
			List<String> list = Arrays.asList(userIds.split(","));
			userGroupUserMapper.deleteUser(list, userGroupId);
			success = true;
		}

		json.put("success", success);
		json.put("reason", reason);
		return json;
	}

	@Override
	public Object doBatchAddUserGroup(String userId, String userGroupIds) {
		logger.info("saveBatchUserForRole,userId:{},userGroupIds:{}",userId,userGroupIds);
		Map<String, Object> json = new HashMap<String, Object>();
		boolean success = false;
		String reason = "";
		if (StringUtils.isEmpty(userId) || StringUtils.isEmpty(userGroupIds)) {
			reason = "参数错误!";
		} else {
			List<String> listgid = new ArrayList<String>(Arrays.asList(userGroupIds.split(";")));
			for (String gId : listgid) {
				UserGroupUser ugu = new UserGroupUser();
				ugu.setUserGroupId(new BigDecimal(gId));
				ugu.setUserId(new BigDecimal(userId));
				if (userGroupUserMapper.select(ugu).isEmpty()) {
					userGroupUserMapper.insert(ugu);
				}
			}
			success = true;
		}
		json.put("success", success);
		json.put("reason", reason);
		return json;
	}

	@Override
	public Object deleteUserGroupForUser(String userId, String userGroupIds) {
		logger.info("deleteForRole,userId:{},userGroupIds:{}",userId,userGroupIds);
		Map<String, Object> json = new HashMap<String, Object>();
		boolean success = false;
		String reason = "";
		if (StringUtils.isEmpty(userId) || StringUtils.isEmpty(userGroupIds)) {
			reason = "参数错误!";
		} else {
			List<String> list = Arrays.asList(userGroupIds.split(","));
			userGroupUserMapper.deleteUserGroupForUser(userId, list);
			success = true;
		}

		json.put("success", success);
		json.put("reason", reason);
		return json;
	}

}
